package com.zfei.gfb.util.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.time.Duration;

/**
 * @author fgh
 */
public class GsonBox {

    public static final Gson BASE = newGsonBuilder()
            .create();

    public static final Gson EXPOSE = newGsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .create();

    public static final Gson PRIVATE = newGsonBuilder()
            .setExclusionStrategies(new GsonIgnoreStrategy(GsonView.PRIVATE))
            .create();

    public static final Gson PROTECTED = newGsonBuilder()
            .setExclusionStrategies(new GsonIgnoreStrategy(GsonView.PRIVATE, GsonView.PROTECTED))
            .create();

    public static final Gson PUBLIC = newGsonBuilder()
            .setExclusionStrategies(new GsonIgnoreStrategy(GsonView.PRIVATE, GsonView.PROTECTED, GsonView.PUBLIC))
            .create();

    public static final Gson INTERNAL = newGsonBuilder()
            .setExclusionStrategies(new GsonIgnoreStrategy(GsonView.INTERNAL))
            .create();

    public static final Gson INTERNAL_PRIVATE = newGsonBuilder()
            .setExclusionStrategies(new GsonIgnoreStrategy(GsonView.INTERNAL, GsonView.PRIVATE))
            .create();

    public static final Gson INTERNAL_PROTECTED = newGsonBuilder()
            .setExclusionStrategies(new GsonIgnoreStrategy(GsonView.INTERNAL, GsonView.PRIVATE, GsonView.PROTECTED))
            .create();

    public static final Gson INTERNAL_PUBLIC = newGsonBuilder()
            .setExclusionStrategies(new GsonIgnoreStrategy(GsonView.INTERNAL, GsonView.PRIVATE, GsonView.PROTECTED, GsonView.PUBLIC))
            .create();

    public static GsonBuilder newGsonBuilder() {
        return new GsonBuilder()
                .setFieldNamingStrategy(JsonFieldNamingPolicy.GsonProperty)
                .registerTypeAdapter(byte[].class, new Base64TypeAdapter().nullSafe())
                .registerTypeAdapter(Duration.class, new DurationTypeAdapter().nullSafe());
    }
}
